function handles = array2DplotEP(handles)
% array2DplotEP
%
% private version for mapAnalysis2p0
%
% See also array2Dplot
%
% Editing:
% gs april 2005
% ------------------------------------------------------

% initialize some variables
array = handles.data.map.mapActive.bsArray;
[rows,cols] = size(array);
totalTime = (rows-1)/handles.data.acq.sampleRate;
xTimeAxis = linspace(0, totalTime, rows)'; 
traceAxis = [1:cols];

% set up the figure
x = .17; 
y = .08; 
w = .7; 
h = .7; 
% x = 100; y = 30; w = 1000; h = 1090;
figure('Units', 'normalized', 'Position', [x y w h], 'Name', 'array2Dplot', ...
    'NumberTitle', 'off', 'Color', 'w', 'DoubleBuffer', 'on');
subplotRows = 2; subplotCols = 3; plotnum = 0;
colormap(jet2);
% if ~handles.data.analysis.currentClamp & ~handles.data.analysis.responsesInvertedFlag % commented out; GS20060530
%     colormap(flipud(colormap(jet2)));
    colormap(jet2(256));

% end

% Laser intensity
plotnum = plotnum+1;
hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
X = 1:length(handles.data.map.mapActive.laserIntensity);
plot(X, handles.data.map.mapActive.laserIntensity, 'co-');
set(gca, 'YLim', [0 max(get(gca, 'YLim'))], 'XLim', [0 max(X)]);
xlabel('Trace');
ylabel('mW');
legend('laser', 0);

% image of slice (in mapping position) -----------------------
plotnum = plotnum+1;
try
    hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
    handles.data.analysis.hSliceImg = imagesc(handles.data.image.img);
    daspect([1 1 1]);
    % % set(himg, 'ButtonDownFcn', 'addSliceImage');
    % set(gca, 'ButtonDownFcn', 'addSliceImage');
    % title('Click to upload an image');
    title(handles.data.image.imgName);
    xrange = handles.data.acq.imageXrange;
    yrange = handles.data.acq.imageYrange;
    set(handles.data.analysis.hSliceImg, ...
        'XData', [-xrange/2 xrange/2], 'YData', [-yrange/2 yrange/2]);
    axis tight;
catch
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MAP IMAGES - GENERAL SETTINGS
% % lens = state.uncaging.analysis.lens;
% % switch lens
% % case '10x Water'
% %     magFac = 375;
% % case '4x Air'
% %     magFac = 750;
% % end
[sizeX sizeY z] = size(handles.data.map.mapActive.mapDataArray(:,:,1));
magFacX = (handles.data.map.mapActive.mapSpacing * (sizeX-1))/2;
magFacY = (handles.data.map.mapActive.mapSpacing * (sizeY-1))/2;
plotNameTxtPos = [0 1.05];
colorBarTxtPos = [1.13 -.08];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% plot of all traces, superimposed
plotnum = plotnum+1;
hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
plot(xTimeAxis, array);
set(gca, 'XLim', [0 max(xTimeAxis)]);
xlabel('Seconds');
ylabel('Amplitude');
stimOn = handles.data.analysis.stimOnEP;
line([stimOn stimOn], [min(get(gca,'YLim')) max(get(gca,'YLim'))], 'LineStyle', '-'); 
epEnd = stimOn + handles.data.analysis.responseDurEP;
line([epEnd epEnd], [min(get(gca,'YLim')) max(get(gca,'YLim'))], 'LineStyle', '-');

% TODO: add a line for the AP threshold

% Map of #APs per pixel 
plotnum = plotnum+1;
hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
apMap = handles.data.map.mapActive.mapDataArray(:,:,6);
result = apMap;
imagesc(result, 'XData', [-magFacX magFacX], 'YData', [-magFacY magFacY]);
% if ~handles.data.analysis.currentClamp
%     lowerLim = min(min(result));
%     upperLim = max(max(result(result<inf)));
%     upperLimTweaked = upperLim + 0.02*(abs(upperLim-lowerLim));
%     set(gca, 'CLim', [lowerLim upperLimTweaked]);
% else
% end
set(gca, 'PlotBoxAspectRatio', [1 1 1]);
xlabel('micrometers');
ylabel('micrometers');
text(plotNameTxtPos(1), plotNameTxtPos(2),'Number of APs', ...
    'Units', 'Normalized', 'FontWeight', 'Bold');
colorbar('vert');
text(colorBarTxtPos(1), colorBarTxtPos(2),'pA', 'Units', 'Normalized');

% genOnset map
plotnum = plotnum+1;
hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
apMap = handles.data.map.mapActive.mapDataArray(:,:,7);
result = apMap;
imagesc(result, 'XData', [-magFacX magFacX], 'YData', [-magFacY magFacY]);
% if ~handles.data.analysis.currentClamp
    lowerLim = min(min(result));
    upperLim = max(max(result(result<inf)));
    upperLimTweaked = upperLim + 0.02*(abs(upperLim-lowerLim));
    try
        set(gca, 'CLim', [lowerLim upperLimTweaked]);
    catch
    end
% else
% end
set(gca, 'PlotBoxAspectRatio', [1 1 1]);
xlabel('micrometers');
text(plotNameTxtPos(1), plotNameTxtPos(2),'Onset', ...
    'Units', 'Normalized', 'FontWeight', 'Bold');
hcb = colorbar('vert');
text(colorBarTxtPos(1), colorBarTxtPos(2), '(sec)', 'Units', 'Normalized');

return
% ---------- AMPLITUDE histogram ------------------------------
% 
% % obtain map of direct responses to exclude them from the histograms
% directMap = handles.data.map.mapActive.mapDataArray(:,:,3);
% directMap(directMap~=0) = NaN;
% directMap(directMap==0) = 1; % ends up with NaNs at direct sites, 1's everywhere else
% 
% % histo params
% leftLim = -9;
% binWidth = .5;
% rightLim = 74;
% histoEdges = [-inf  (leftLim:binWidth:rightLim)  inf];
% histoEdges2 = [leftLim-1*binWidth:binWidth:rightLim+1*binWidth];
% 
% % for each baseline and synaptic map, obtain the histograms of the data
% n=1;    
% % histo of the baseline window data
% b = handles.data.map.mapActive.mapDataArray(:,:,13);
% b = b.*directMap; % convert directs to NaNs
% c = reshape(b, 1, numel(b)); % vectorize
% baselineData(n, :) = c(~isnan(c)); % exclude NaNs
% baselineN(n, :) = histc(baselineData(n, :), histoEdges);
% %
% % use the baseline noise level to generate a thresholded binary map of responses
% baselineSD(n) = std(baselineData(n, :));
% d = handles.data.map.mapActive.mapDataArray(:,:,14) > 2 * baselineSD(n); % 0 = null, 1 = synaptic response
% d = d.*directMap;           % NaN = direct response
% d(isnan(d))=-inf;           % NaN => inf for plotting
% d = -d;
% 
% % histo of the synaptic window data 
% s = handles.data.map.mapActive.mapDataArray(:,:,14);
% s = s.*directMap;
% c = reshape(s, 1, numel(s));
% synapticData(n, :) = c(~isnan(c));
% synapticN(n, :) = histc(synapticData(n, :), histoEdges);
% 
% plotnum = plotnum+1;
% hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
% h = bar(histoEdges2, baselineN, 'histc');
% set(h, 'FaceColor', 'k', 'LineStyle', 'none');
% hold on;
% h = bar(histoEdges2, synapticN, 'histc');
% set(h, 'FaceColor', 'none', 'EdgeColor', 'r', 'LineWidth', [1]);
% set(gca, 'XLim', [-15 80]);
% xlabel('Amplitude (pA)');
% ylabel('N');
% legend('baseline', 'synaptic');
% % legend boxoff;
% 
% % binarized/thresholded map (from amplitude histogram, 2SDs)
% plotnum = plotnum+1;
% hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
% himage = imagesc(d);
% set(gca, 'CLim', [-1 0.03], 'PlotBoxAspectRatio', [1 1 1], 'Visible', 'off');

% LATENCY histogram *************************

% % data for histogram -- from "All, Onset" array -- i.e., from genOnset map below
% if ~handles.data.analysis.currentClamp
%     if ~handles.data.analysis.responsesInvertedFlag
%         genMap = handles.data.map.mapActive.mapDataArray(:,:,10);
%     else
%         genMap = handles.data.map.mapActive.mapDataArray(:,:,12);
%     end
%     genMap(genMap==0) = inf;
%     result = genMap;
%     result(result==0) = inf;
% else
%     apMap = handles.data.map.mapActive.mapDataArray(:,:,6);
%     result = apMap;
% end

result = handles.data.map.mapActive.mapDataArray(:,:,6);

% histo params
leftLim = 0;
binWidth = .001;
rightLim = handles.data.analysis.responseDurEP;
histoEdges = [leftLim : binWidth : rightLim];

% for each baseline and synaptic map, obtain the histograms of the data
% n=1;    
% histo of the baseline window data
b = result;
b(b == inf) = NaN;
c = reshape(b, 1, numel(b)); % vectorize
histoData = c(~isnan(c)); % exclude NaNs
% histoData
% histoEdges
% size(histoData)
% size(histoEdges)
histoN = histc(histoData, histoEdges);

plotnum = plotnum+1;
hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
h = bar(histoEdges, histoN, 'histc');
set(h, 'FaceColor', 'r', 'LineStyle', 'none');
set(gca, 'XLim', [leftLim rightLim]);
xlabel('Onset latency (sec)');
ylabel('Number');
edEnd = handles.data.analysis.directWindow;
hline = line([edEnd edEnd], [min(get(gca,'YLim')) max(get(gca,'YLim'))], ...
    'Color', 'b', 'LineStyle', ':'); 
handles.data.map.mapStack.mapStackLatHistoEdges = histoEdges';
if isempty(handles.data.map.mapStack.mapStackLatHistoN)
    handles.data.map.mapStack.mapStackLatHistoN = histoN';
else
    handles.data.map.mapStack.mapStackLatHistoN = [handles.data.map.mapStack.mapStackLatHistoN histoN'];
end


% titleStr = [state.uncaging.map.uncagingPathName(end-11:end-7) ', ' ...
%         state.uncaging.map.uncagingPathName(end-5:end-1)];
% if strcmp(state.uncaging.map.uncagingPathName(end-11:end-7), 'races')
%     titleStr = [state.uncaging.map.uncagingPathName(end-18:end-14) ', ' ...
%             state.uncaging.map.uncagingPathName(end-5:end-1)];
% end
% text('String', titleStr, 'Units', 'Normalized', 'Position', [0 1.2], ...
%     'FontWeight', 'Bold', 'Parent', hsub(1));
% 
% text('String', 'All', 'Units', 'Normalized', 'Position', [-.5 0.3], ...
%     'FontWeight', 'Bold', 'Parent', hsub(9), 'Rotation', 90);
% 
% text('String', 'Non-Direct', 'Units', 'Normalized', 'Position', [-.5 0.35], ...
%     'FontWeight', 'Bold', 'Parent', hsub(13), 'Rotation', 90);
% 
% if length(hsub) > 16
%     text('String', 'Direct', 'Units', 'Normalized', 'Position', [-.5 0.4], ...
%         'FontWeight', 'Bold', 'Parent', hsub(17), 'Rotation', 90);
% end
